package com.lxp.project.function.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.lxp.common.constant.HttpStatus;
import com.lxp.common.utils.StringUtils;
import com.lxp.common.utils.poi.ExcelUtil;
import com.lxp.framework.web.domain.AjaxResult;
import com.lxp.framework.web.page.TableDataInfo;
import com.lxp.project.function.domain.FnProduct;
import com.lxp.project.function.mapper.FnProductMapper;
import org.springframework.stereotype.Service;
import com.lxp.project.function.mapper.FnProjectMapper;
import com.lxp.project.function.domain.FnProject;
import com.lxp.project.function.service.IFnProjectService;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目管理Service业务层处理
 *
 * @author 乐乐
 * @date 2022-05-04
 */
@Service
public class FnProjectServiceImpl extends ServiceImpl<FnProjectMapper, FnProject> implements IFnProjectService {
    @Resource
    private FnProjectMapper fnProjectMapper;
    @Resource
    private FnProductMapper fnProductMapper;

    @Override
    public List<FnProject> getAllList() {
        return fnProjectMapper.selectList(null);
    }

    @Override
    public TableDataInfo getPageList(FnProject fnProject, TableDataInfo tableSupport) {
        LambdaQueryWrapper<FnProject> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(fnProject.getName())) {
            queryWrapper.like(FnProject::getName, fnProject.getName());
        }
        if (fnProject.getProductId() != null) {
            queryWrapper.eq(FnProject::getProductId, fnProject.getProductId());
        }
        Integer count = fnProjectMapper.selectCount(queryWrapper);
        IPage<FnProject> iPage = fnProjectMapper.selectPage(new Page<>(tableSupport.getPageNum(), tableSupport.getPageSize()), queryWrapper);
        tableSupport.setCode(HttpStatus.SUCCESS);
        tableSupport.setMsg("查询成功");
        tableSupport.setRows(iPage.getRecords());
        tableSupport.setTotal(count);
        return tableSupport;
    }

    @Override
    @Transactional
    public AjaxResult saveData(FnProject fnProject) {
        FnProduct fnProduct = fnProductMapper.selectById(fnProject.getProductId());
        if (fnProduct.getCount() < fnProject.getProductCount()) {
            return AjaxResult.error("库存不足");
        }
        fnProduct.setCount(fnProduct.getCount() - fnProject.getProductCount());
        fnProductMapper.updateById(fnProduct);
        return AjaxResult.msg(fnProjectMapper.insert(fnProject));
    }

    @Override
    public AjaxResult updateDataById(FnProject fnProject) {
        return AjaxResult.msg(fnProjectMapper.updateById(fnProject));
    }

    @Override
    public AjaxResult removeByIds(List<Long> ids) {
        return AjaxResult.msg(fnProjectMapper.deleteBatchIds(ids));
    }

    @Override
    public AjaxResult exports(FnProject fnProject) {
        LambdaQueryWrapper<FnProject> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(fnProject.getName())) {
            queryWrapper.like(FnProject::getName, fnProject.getName());
        }
        if (fnProject.getProductId() != null) {
            queryWrapper.eq(FnProject::getProductId, fnProject.getProductId());
        }
        List<FnProject> list = fnProjectMapper.selectList(queryWrapper);
        ExcelUtil<FnProject> util = new ExcelUtil<>(FnProject.class);
        return util.exportExcel(list, "fnProject");
    }

    @Override
    public FnProject getById(Long id) {
        return fnProjectMapper.selectById(id);
    }
}
